﻿using SymulatorEthernetWPF.Shared;
using SymulatorEthernetWPF.SimObjects.Devices;
using SymulatorEthernetWPF.SimObjects.Layers.DataLink;
using SymulatorEthernetWPF.UserInterface;
using SymulatorEthernetWPF.UserInterface.UIElements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SymulatorEthernetWPF.SimObjects
{
    /// <summary>
    /// Interaction logic for TimetableItem.xaml
    /// </summary>
    public partial class TimetableItem : UserControl
    {
        public DeviceControl deviceControl;
        public MacAdress source;
        public MacAdress destination;
        public int time;
        public int repeat;
        private TaskQueueItem _taskQueueItem=null;

        public delegate void deleteDelegate(TimetableItem timetableItem, TaskQueueItem taskQueueItem);
        public event deleteDelegate deleteEvent;

        public TimetableItem(DeviceControl deviceControl, MacAdress source, MacAdress destination, int time, int repeat)
        {
            InitializeComponent();
            this.deviceControl = deviceControl;
            this.destination = destination;
            this.source = source;
            this.sourceSelectionTextbox.Text = source.ToString();
            this.destinationSelectionTextbox.Text = destination.ToString();
            this.time = time;
            this.repeat = repeat;
            if (repeat != -1)
            {
                DoOnce.IsChecked = false;
                RepeatTextBox.Text = repeat.ToString();
            }
            timeInput.Number = time;
            Height = 0;
            
        }

        public TimetableItem(DeviceControl deviceControl, TaskQueueItem taskQueueItem)
        {
            InitializeComponent();
            this.deviceControl = deviceControl;
            this.destination = taskQueueItem.destination;
            this.source = taskQueueItem.destination;
            this.sourceSelectionTextbox.Text = taskQueueItem.source.ToString();
            this.destinationSelectionTextbox.Text = taskQueueItem.destination.ToString();
            this.time = taskQueueItem.time;
            timeInput.Number = time;
            _taskQueueItem = taskQueueItem;
            activeCheckbox.IsChecked = taskQueueItem.active;
            if (taskQueueItem.repeat != -1)
            {
                DoOnce.IsChecked = false;
                RepeatTextBox.Text = taskQueueItem.repeat.ToString();
            }
            

        }

        public void save()
        {
            source = new MacAdress(sourceSelectionTextbox.Text);
            destination = new MacAdress(destinationSelectionTextbox.Text);
            if (_taskQueueItem == null)
            {
                if (!(bool)DoOnce.IsChecked)
                    _taskQueueItem = new TaskQueueItem(deviceControl.GUID, source, destination, timeInput.Number, RepeatTextBox.Number, (bool)activeCheckbox.IsChecked);
                else
                    _taskQueueItem = new TaskQueueItem(deviceControl.GUID, source, destination, timeInput.Number, -1, (bool)activeCheckbox.IsChecked);
                Simulation.taskQueue.Add(_taskQueueItem);
            }
            else
            {
                _taskQueueItem.active = (bool)activeCheckbox.IsChecked;
                _taskQueueItem.source = source;
                _taskQueueItem.destination = destination;
                _taskQueueItem.time = timeInput.Number;
                if(!(bool)DoOnce.IsChecked)
                    _taskQueueItem.repeat = RepeatTextBox.Number;
                else
                    _taskQueueItem.repeat = -1;
            }
        }

        private void TimetableItem_Loaded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation heightAnimation = new DoubleAnimation();

            Storyboard loadedStory = new Storyboard();

            heightAnimation.From = 0;
            heightAnimation.To = MaxHeight;
            heightAnimation.Duration = TimeSpan.FromSeconds(0.3);
            Storyboard.SetTarget(heightAnimation, this);
            Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(TimetableItem.HeightProperty));
            loadedStory.Children.Add(heightAnimation);
            loadedStory.Begin();

        }

        private void activeCheckbox_Checked(object sender, RoutedEventArgs e)
        {
            this.Background = Brushes.LightGreen;
        }

        private void activeCheckbox_Unchecked(object sender, RoutedEventArgs e)
        {
            this.Background = Brushes.IndianRed;
        }
        private void deleteAnimation()
        {
            DoubleAnimation heightAnimation = new DoubleAnimation();
            DoubleAnimation opacityAnimation = new DoubleAnimation();

            Storyboard deleteStory = new Storyboard();

            heightAnimation.From = this.Height;
            heightAnimation.To = 0;
            heightAnimation.Duration = TimeSpan.FromSeconds(0.3);
            Storyboard.SetTarget(heightAnimation, this);
            Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(TimetableItem.HeightProperty));
            deleteStory.Children.Add(heightAnimation);

            opacityAnimation.From = 1;
            opacityAnimation.To = 0;
            opacityAnimation.Duration = TimeSpan.FromSeconds(0.3);
            Storyboard.SetTarget(opacityAnimation, this);
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(TimetableItem.OpacityProperty));
            deleteStory.Children.Add(opacityAnimation);

            deleteStory.Completed += deleteAnimation_Completed;
            deleteStory.Begin();
        }

        void deleteAnimation_Completed(object sender, EventArgs e)
        {
            if (deleteEvent != null)
                deleteEvent(this, _taskQueueItem);
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            deleteAnimation();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            AdressSelection adressSelection = new AdressSelection(deviceControl, false);
            adressSelection.AdressChanged += adressSelection_SourceAdressChanged;
            adressSelection.ShowDialog();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            AdressSelection adressSelection = new AdressSelection(deviceControl, true);
            adressSelection.AdressChanged += adressSelection_DestinationAdressChanged;
            adressSelection.ShowDialog();
        }
        void adressSelection_SourceAdressChanged(MacAdress adress)
        {
            sourceSelectionTextbox.Text = adress.ToString();
        }
        private void adressSelection_DestinationAdressChanged(MacAdress adress)
        {
            destinationSelectionTextbox.Text = adress.ToString();
        }


    }
}
